//
//  BSAdViewController.m
//  BaiSi
//
//  Created by Sycamore on 16/4/27.
//  Copyright © 2016年 Sycamore. All rights reserved.
//

#import "BSAdViewController.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import "BSADItem.h"
#import <UIImageView+WebCache.h>
#import "BSTabBarController.h"
@interface BSAdViewController ()
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIButton *timeButton;

@property (weak, nonatomic) IBOutlet UIImageView *launchImageView;
@property (nonatomic,strong) BSADItem *item;
@property (nonatomic,weak) NSTimer *timer;
@end

@implementation BSAdViewController

//跳转到主框架
- (IBAction)jump:(UIButton *)sender {
    
    BSTabBarController *tabBarVC = [[BSTabBarController alloc]init];
    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarVC;
    
    //记住定时器不用的时候一定要释放掉
    [_timer invalidate];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //设置图片,注意屏幕适配
    [self setLaunchImage];
    
    
    NSString *urlString = @"http://mobads.baidu.com/cpro/ui/mads.php";
    NSDictionary *param = @{
                            @"code2" : @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"
                            };
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //发送网络请求
    [manager GET:urlString parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary*  _Nullable responseObject) {
//        NSLog(@"%@",responseObject);
        [responseObject writeToFile:@"/Users/Sycamore/Desktop/BaiSi/ad.plist" atomically:YES];
        
        //字典转模型
        NSArray *adArray = responseObject[@"ad"];
        _item = [BSADItem mj_objectWithKeyValues:adArray[0]];
        
        //设置广告图片
        [self setUpAddImageWithItem:_item];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
    //不管请求成功还是失败，都要开启定时器
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(minus) userInfo:nil repeats:YES];
}

- (void)minus
{
    static int i = 2;
    
    _timeButton.titleLabel.text = [NSString stringWithFormat:@"  倒数(%d)",i];
    
    //减到零的时候，跳到下一个控制器
    if (i == -1) {
        [self jump:_timeButton];
    }
    
    i--;
}


- (void)setUpAddImageWithItem:(BSADItem *)item
{
    UIImageView *adView = [[UIImageView alloc]init];
    adView.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [adView addGestureRecognizer:tap];
    [_contentView insertSubview:adView belowSubview:_timeButton];
    
    //设置frame
    
    if (item.w) {
        CGFloat h = BSScreenWidth / item.w * item.h;
        adView.frame = CGRectMake(0, 0, BSScreenWidth, h);
    }
    
    [adView sd_setImageWithURL:[NSURL URLWithString:item.w_picurl]];
    
}

- (void)tap
{
    NSURL *url = [NSURL URLWithString:_item.ori_curl];
    if ([[UIApplication sharedApplication]canOpenURL:url]) {
            [[UIApplication sharedApplication]openURL:url];
    }

}

//设置启动图片
- (void)setLaunchImage
{
    if (IOS6P) {
        _launchImageView.image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
    }
    else if (IOS6)
    {
        _launchImageView.image = [UIImage imageNamed:@"LaunchImage-800-667h"];
    }else if (IOS5){
        _launchImageView.image = [UIImage imageNamed:@"LaunchImage-568h"];
    }else{
        _launchImageView.image = [UIImage imageNamed:@"LaunchImage"];
    }
}


@end
